package com.example.plat.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.plat.enity.Activity;
import com.example.plat.enity.dto.ActivityCardDto;
import com.example.plat.enity.dto.ActivityCreateDto;
import com.example.plat.enity.dto.ActivityDetailDto;
import com.example.plat.exception.ActivityException;
import com.example.plat.service.ActivityService;
import com.example.plat.service.PlaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(description = "活动管理")
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private ActivityService service;

    @Autowired
    private PlaceService placeService;

    @ApiOperation("发布活动")
    @PostMapping
    public ResponseEntity<ActivityCardDto> createActivity(@RequestBody ActivityCreateDto dto) throws ActivityException {
        placeService.createPlace(dto.getPlace());

        Activity newActivity = modelMapper.map(dto, Activity.class);
        newActivity.setPhoto(dto.getImgList().get(0));
        newActivity.setPlace(dto.getPlace().getName());
        newActivity.setPlaceId(dto.getPlace().getId());

        Activity res = service.createActivity(newActivity,dto.getImgList());

        return ResponseEntity.ok().body(modelMapper.map(res,ActivityCardDto.class));
    }

    @ApiOperation("参与活动")
    @PostMapping("/{userId}")
    public ResponseEntity<String> joinActivity(@ApiParam("用户ID")@PathVariable Long userId,
                                               Long actId) throws ActivityException {
        String s = service.joinActivity(userId, actId);

        return ResponseEntity.ok(s);
    }

    @ApiOperation("修改活动")
    @PutMapping
    public ResponseEntity<String> updateActivity(@RequestBody ActivityCreateDto activity) throws ActivityException {
        placeService.updatePlace(activity.getPlace());

        Activity newActivity = modelMapper.map(activity, Activity.class);
        newActivity.setPhoto(activity.getImgList().get(0));
        newActivity.setPlace(activity.getPlace().getName());
        newActivity.setPlaceId(activity.getPlace().getId());

        service.updateActivity(newActivity,activity.getImgList());

        return ResponseEntity.ok("修改成功！");
    }

    @ApiOperation("删除活动")
    @DeleteMapping("/{actId}")
    public ResponseEntity<String> deleteActivity(@ApiParam("活动ID")@PathVariable Long actId) throws ActivityException{
        service.deleteActivity(actId);
        return ResponseEntity.ok("删除活动成功！");
    }

    @ApiOperation("取消参与活动")
    @DeleteMapping("/cancel/{actId}")
    public ResponseEntity<String> cancelJoin(@ApiParam("活动ID")@PathVariable Long actId, Long userId) throws ActivityException{
        service.cancelJoin(userId,actId);
        return ResponseEntity.ok("取消参与活动成功！");
    }

    @ApiOperation("收藏活动")
    @PostMapping("/collect/{userId}")
    public ResponseEntity<String> collectActivity(@ApiParam("用户ID")@PathVariable Long userId,
                                                  Long actId) throws ActivityException {
        service.collect(userId,actId);
        return ResponseEntity.ok("success!");
    }

    @ApiOperation("取消收藏活动")
    @DeleteMapping("/{userId}/{actId}")
    public ResponseEntity<String> cancelCollectActivity(@ApiParam("用户ID")@PathVariable Long userId,
                                                    @ApiParam("活动ID")@PathVariable Long actId) throws ActivityException {
        service.cancleCollect(userId,actId);
        return ResponseEntity.ok("success!");
    }
}
